<?php

namespace App\Admin\Controllers\Goods;

use App\Models\Goods;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class GoodsController extends Controller
{
    use ModelForm;
    protected $header = '商品列表';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Goods::class, function (Grid $grid) {
            $grid->model()->select([
                'id',
                'goods_name',
                'goods_img',
                'goods_sort',
                'goods_price',
                'goods_stock',
                'goods_sales',
                'examine_status',
                'is_recommend',
                'is_up',
                'goods_number',
                'created_at',
                'updated_at'
            ])->orderBy('goods_sort','asc');
            $grid->id('编号')->sortable();
            $grid->goods_img('商品图片')->image('','50','50');
            $grid->goods_name('商品名称');
            $grid->goods_price('积分');
            $grid->column('标签')->switchGroup([
                'is_up' => '上架',
                'is_recommend' => '推荐',
            ], [
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ]);
            $grid->goods_sort('排序');
            $grid->goods_stock('库存')->editable();
            $grid->goods_sales('销量');
            $grid->examine_status('审核状态')->using(['1' => '待审核', '2' => '审核通过', '3' => '审核未通过']);
            $grid->created_at('创建时间');

            //搜索栏
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('goods_name','商品名称');
                $filter->equal('is_up', '是否上架')->select([
                    '1'=>'是',
                    '0'=>'否'
                ]);
                $filter->equal('is_recommend', '是否推荐')->select([
                    '1'=>'是',
                    '0'=>'否'
                ]);
                $filter->equal('is_recommend', '审核状态')->select([
                    '1' => '待审核',
                    '2' => '审核通过',
                    '3' => '审核未通过'
                ]);
                $filter->between('created_at', '创建时间')->datetime();
            });
            //操作列
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Goods::class, function (Form $form) {

            $form->tools(function (Form\Tools $tools) {
                // 去掉`查看`按钮
                $tools->disableView();
            });

            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
            });

            $form->display('id', '编号');
            $form->text('goods_name', '商品名称')->rules('required');
            $form->image('goods_img','图片')->setWidth(4);

            $form->editor('details', '商品介绍');

            $form->number('goods_price', '积分')->min(1);
            $form->number('goods_stock', '库存')->min(0);
            $form->switch('is_up', '是否上架')->states([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ]);
            $form->switch('is_recommend', '是否推荐')->states([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ]);

            /*$form->saving(function ($form) {

                $success = new MessageBag([
                    'title'   => 'title...',
                    'message' => 'message....',
                ]);

                return back()->with(compact('success'));
            });*/

            /*$form->saved(function (Form $form) {

                $detail = Goods\Detail::where('goods_id', $form->model()->id)->first();

                if ($detail){
                    $detail->details = $form->details;
                    $detail->update();
                } else {
                    $detail = new Goods\Detail;
                    $detail->goods_id = $form->model()->id;
                    $detail->details = $form->details;
                    $detail->save();
                }
            });*/

        });
    }


}
